package com.etc.controller;

import com.etc.entity.CommentResult;
import com.etc.entity.Payment;
import com.etc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PaymentController {
    @Autowired
    private PaymentService paymentService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private ParkService parkService;

    @Autowired
    private BuildingService buildingService;

    @Autowired
    private HouseholdService householdService;

    @Autowired
    private OwnerService ownerService;


    @RequestMapping("payment/findAllPayment")
    public CommentResult findAllPayment(){
        List<Payment> list = paymentService.findAllPayment();
        List<Map<String,Object>> reslult = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();

            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getId());
            map.put("building_name",buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            map.put("building_id",buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getId());
            map.put("room_num",roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map.put("room_id",roomService.findById(list.get(i).getRoom_id()).getId());
            if (householdService.findByRoom(list.get(i).getRoom_id()).size() ==0){
                map.put("household_name",null);
                map.put("household_id",null);
                map.put("household_phone",null);
            }else{
                map.put("household_name",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getName());
                map.put("household_id",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getId());
                map.put("household_phone",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getPhone());
            }
            if (roomService.findById(list.get(i).getRoom_id()).getOwner_id() == null){
                map.put("owner_name",null);
                map.put("owner_id",null);
                map.put("owner_phone",null);
            }
            else {
                map.put("owner_name",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getName());
                map.put("owner_id",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getId());
                map.put("owner_phone",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getPhone());
            }

            map.put("services",list.get(i).getServices());
            map.put("amount",list.get(i).getAmount());
            map.put("pubdate",list.get(i).getPubdate());
            map.put("deadline",list.get(i).getDeadline());
            map.put("status",list.get(i).getStatus());
            map.put("remark",list.get(i).getRemark());
            reslult.add(map);
        }
        return new CommentResult(200,"success",reslult);
    }

    @RequestMapping("payment/findById/{id}")
    public CommentResult findById(@PathVariable("id") Integer id){
        Payment result = paymentService.findById(id);
        List<Map<String,Object>> list = new ArrayList<>();
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",result.getId());
            map.put("park_name",parkService.findById(roomService.findById(result.getRoom_id()).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(roomService.findById(result.getRoom_id()).getPark_id()).getId());
            map.put("building_name",buildingService.findById(roomService.findById(result.getRoom_id()).getBuilding_id()).getName());
            map.put("building_id",buildingService.findById(roomService.findById(result.getRoom_id()).getBuilding_id()).getId());
            map.put("room_num",roomService.findById(result.getRoom_id()).getRoom_num());
            map.put("room_id",roomService.findById(result.getRoom_id()).getId());

            if (householdService.findByRoom(result.getRoom_id()).size() ==0){
                map.put("household_name",null);
                map.put("household_id",null);
                map.put("household_phone",null);
            }else{
                map.put("household_name",householdService.findByRoom(result.getRoom_id()).get(0).getName());
                map.put("household_id",householdService.findByRoom(result.getRoom_id()).get(0).getId());
                map.put("household_phone",householdService.findByRoom(result.getRoom_id()).get(0).getPhone());
            }
        if (roomService.findById(result.getRoom_id()).getOwner_id() == null){
            map.put("owner_name",null);
            map.put("owner_id",null);
            map.put("owner_phone",null);
        }
        else {
            map.put("owner_name",ownerService.findById(roomService.findById(result.getRoom_id()).getOwner_id()).getName());
            map.put("owner_id",ownerService.findById(roomService.findById(result.getRoom_id()).getOwner_id()).getId());
            map.put("owner_phone",ownerService.findById(roomService.findById(result.getRoom_id()).getOwner_id()).getPhone());
        }
            map.put("services",result.getServices());
            map.put("amount",result.getAmount());
            map.put("pubdate",result.getPubdate());
            map.put("deadline",result.getDeadline());
            map.put("status",result.getStatus());
            map.put("remark",result.getRemark());
            list.add(map);

        return new CommentResult(200,"success",list);

    }

    @RequestMapping("payment/findByRoom/{id}")
    public CommentResult findByRoom(@PathVariable("id") Integer id){
        List<Payment> list = paymentService.findByRoom(id);
        List<Map<String,Object>> reslult = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();

            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getId());
            map.put("building_name",buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            map.put("building_id",buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getId());
            map.put("room_num",roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map.put("room_id",roomService.findById(list.get(i).getRoom_id()).getId());
            if (householdService.findByRoom(list.get(i).getRoom_id()).size() ==0){
                map.put("household_name",null);
                map.put("household_id",null);
                map.put("household_phone",null);
            }else{
                map.put("household_name",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getName());
                map.put("household_id",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getId());
                map.put("household_phone",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getPhone());
            }
            if (roomService.findById(list.get(i).getRoom_id()).getOwner_id() == null){
                map.put("owner_name",null);
                map.put("owner_id",null);
                map.put("owner_phone",null);
            }
            else {
                map.put("owner_name",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getName());
                map.put("owner_id",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getId());
                map.put("owner_phone",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getPhone());
            }
            map.put("services",list.get(i).getServices());
            map.put("amount",list.get(i).getAmount());
            map.put("pubdate",list.get(i).getPubdate());
            map.put("deadline",list.get(i).getDeadline());
            map.put("status",list.get(i).getStatus());
            map.put("remark",list.get(i).getRemark());
            reslult.add(map);
        }
        return new CommentResult(200,"success",reslult);
    }

    @RequestMapping("payment/findByStatus/{status}")
    public CommentResult findByStatus(@PathVariable("status") Integer status){
        List<Payment> list = paymentService.findByStatus(status);
        List<Map<String,Object>> reslult = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();

            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getId());
            map.put("building_name",buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            map.put("building_id",buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getId());
            map.put("room_num",roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map.put("room_id",roomService.findById(list.get(i).getRoom_id()).getId());
            if (householdService.findByRoom(list.get(i).getRoom_id()).size() ==0){
                map.put("household_name",null);
                map.put("household_id",null);
                map.put("household_phone",null);
            }else{
                map.put("household_name",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getName());
                map.put("household_id",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getId());
                map.put("household_phone",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getPhone());
            }
            if (roomService.findById(list.get(i).getRoom_id()).getOwner_id() == null){
                map.put("owner_name",null);
                map.put("owner_id",null);
                map.put("owner_phone",null);
            }
            else {
                map.put("owner_name",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getName());
                map.put("owner_id",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getId());
                map.put("owner_phone",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getPhone());
            }
            map.put("services",list.get(i).getServices());
            map.put("amount",list.get(i).getAmount());
            map.put("pubdate",list.get(i).getPubdate());
            map.put("deadline",list.get(i).getDeadline());
            map.put("status",list.get(i).getStatus());
            map.put("remark",list.get(i).getRemark());
            reslult.add(map);
        }
        return new CommentResult(200,"success",reslult);
    }

    //查询逾期缴费信息
    @RequestMapping("payment/findAllExceed")
    public CommentResult findAllExceed()
    {
        List<Payment> list = paymentService.findAllExceed();
        List<Map<String,Object>> reslult = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();

            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getId());
            map.put("building_name",buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            map.put("building_id",buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getId());
            map.put("room_num",roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map.put("room_id",roomService.findById(list.get(i).getRoom_id()).getId());
            if (householdService.findByRoom(list.get(i).getRoom_id()).size() ==0){
                map.put("household_name",null);
                map.put("household_id",null);
                map.put("household_phone",null);
            }else{
                map.put("household_name",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getName());
                map.put("household_id",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getId());
                map.put("household_phone",householdService.findByRoom(list.get(i).getRoom_id()).get(0).getPhone());
            }
            if (roomService.findById(list.get(i).getRoom_id()).getOwner_id() == null){
                map.put("owner_name",null);
                map.put("owner_id",null);
                map.put("owner_phone",null);
            }
            else {
                map.put("owner_name",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getName());
                map.put("owner_id",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getId());
                map.put("owner_phone",ownerService.findById(roomService.findById(list.get(i).getRoom_id()).getOwner_id()).getPhone());
            }
            map.put("services",list.get(i).getServices());
            map.put("amount",list.get(i).getAmount());
            map.put("pubdate",list.get(i).getPubdate());
            map.put("deadline",list.get(i).getDeadline());
            map.put("status",list.get(i).getStatus());
            map.put("remark",list.get(i).getRemark());
            reslult.add(map);
        }
        return new CommentResult(200,"success",reslult);
    }
}
